<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>PyMOTW: collections &mdash; PyMOTW Document v1.6 documentation</title>
    <link rel="stylesheet" href="../_static/mytest.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.6',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    
    <!-- comment utility -->
    <link rel="stylesheet" href="../_static/comment.css" type="text/css" />
    <script type="text/javascript" src="../_static/md5.js"></script>
    <script type="text/javascript" src="../_static/comment.js"></script>
    <!-- end -->
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="PyMOTW Document v1.6 documentation" href="../index.html" />
    <link rel="next" title="PyMOTW: fileinput" href="fileinput.html" />
    <link rel="prev" title="PyMOTW: atexit" href="atexit.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="fileinput.html" title="PyMOTW: fileinput"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="atexit.html" title="PyMOTW: atexit"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">PyMOTW Document v1.6 documentation</a> &raquo;</li> 
      </ul>
    </div> 
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">PyMOTW: collections</a><ul>
<li><a class="reference external" href="#id1">双端队列</a></li>
<li><a class="reference external" href="#defaultdict">defaultdict</a></li>
<li><a class="reference external" href="#id2">参考</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="atexit.html"
                                  title="previous chapter">PyMOTW: atexit</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="fileinput.html"
                                  title="next chapter">PyMOTW: fileinput</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/documents/collections.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div> 

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="pymotw-collections">
<h1>PyMOTW: collections<a class="headerlink" href="#pymotw-collections" title="Permalink to this headline">¶</a></h1>
<p>collections模块包含了一些除了内置类型, 如列表, 字典外的容器数据类型.</p>
<ul class="simple">
<li>模块: collections</li>
<li>目的: 数据类型的包含容器.</li>
<li>Python 版本: 2.4 +</li>
</ul>
<div class="section" id="id1">
<h2>双端队列<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
<p>一个双头队列, 或者&#8221;双端队列&#8221;, 支持从每一端上增加和删除元素. 更常用的像栈和队列, 它们可看成是双端队列的特殊情况, 即被限制为输入和输出只能从一端进行.</p>
<p>因为双端队列是一种序列容器, 所以它们支持一些列表也支持的相同操作, 如利用__getitem__()检查内部元素, 计算长度, 根据标识符的匹配与否来移除某个元素.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">collections</span>

<span class="n">d</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">(</span><span class="s">&#39;abcdefg&#39;</span><span class="p">)</span>

<span class="k">print</span> <span class="s">&#39;Deque:&#39;</span><span class="p">,</span> <span class="n">d</span>
<span class="k">print</span> <span class="s">&#39;Length:&#39;</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&#39;Left end:&#39;</span><span class="p">,</span> <span class="n">d</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span>
<span class="k">print</span> <span class="s">&#39;Right end:&#39;</span><span class="p">,</span> <span class="n">d</span><span class="p">[</span><span class="o">-</span><span class="mf">1</span><span class="p">]</span>


<span class="n">d</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s">&#39;c&#39;</span><span class="p">)</span>

<span class="k">print</span> <span class="s">&#39;remove(c):&#39;</span><span class="p">,</span> <span class="n">d</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python collections_deque.py
Deque: deque(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
Length: 7
Left end: a
Right end: g
remove(c): deque(['a', 'b', 'd', 'e', 'f', 'g'])</pre>
</div>
<p>一个双端队列可以从每一端填入元素, 在Python实现中使用词语 &#8220;left&#8221; 和 &#8220;right&#8221;.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">collections</span>

<span class="c"># Add to the right ## 增加到右边, 使用extend和append</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">()</span>
<span class="n">d</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="s">&#39;abcdefg&#39;</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&#39;extend :&#39;</span><span class="p">,</span> <span class="n">d</span>
<span class="n">d</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;h&#39;</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&#39;append :&#39;</span><span class="p">,</span> <span class="n">d</span>

<span class="c"># Add to the left ## 增加到左边, 使用extendleft和appendleft</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">()</span>
<span class="n">d</span><span class="o">.</span><span class="n">extendleft</span><span class="p">(</span><span class="s">&#39;abcdefg&#39;</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&#39;extendleft:&#39;</span><span class="p">,</span> <span class="n">d</span>
<span class="n">d</span><span class="o">.</span><span class="n">appendleft</span><span class="p">(</span><span class="s">&#39;h&#39;</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&#39;appendleft:&#39;</span><span class="p">,</span> <span class="n">d</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">extendleft()将对所有的输入进行, 其执行效果等价于对每一个元素进行appendleft(). 最终的结果是这个双端队列包含了一个逆序的输入元素序列.</p>
</div>
<div class="highlight-python"><pre>$ python collections_deque_populating.py
extend : deque(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
append : deque(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
extendleft: deque(['g', 'f', 'e', 'd', 'c', 'b', 'a'])
appendleft: deque(['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a'])</pre>
</div>
<p>类似的, 双端队列可以同时从两端或只从一端获取元素, 具体得看你在算法中是如何写的.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">collections</span>

<span class="k">print</span> <span class="s">&#39;From the right:&#39;</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">(</span><span class="s">&#39;abcdefg&#39;</span><span class="p">)</span>

<span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">print</span> <span class="n">d</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> <span class="c">## 从右</span>
    <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
        <span class="k">break</span>


<span class="k">print</span> <span class="s">&#39;From the left:&#39;</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">(</span><span class="s">&#39;abcdefg&#39;</span><span class="p">)</span>

<span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">print</span> <span class="n">d</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> <span class="c">## 从左</span>
    <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
        <span class="k">break</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python collections_deque_consuming.py
From the right:
g
f
e
d
c
b
a
From the left:
a
b
c
d
e
f
g</pre>
</div>
<p>因为双端队列是线程安全的, 所以你甚至可以在独立线程中从它的两端同时获取元素.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">collections</span>
<span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="n">candle</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">(</span><span class="nb">xrange</span><span class="p">(</span><span class="mf">11</span><span class="p">))</span>

<span class="k">def</span> <span class="nf">burn</span><span class="p">(</span><span class="n">direction</span><span class="p">,</span> <span class="n">nextSource</span><span class="p">):</span>
    <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">next</span> <span class="o">=</span> <span class="n">nextSource</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
            <span class="k">break</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&#39;</span><span class="si">%8s</span><span class="s">: </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">direction</span><span class="p">,</span> <span class="n">next</span><span class="p">)</span>
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="si">%8s</span><span class="s"> done&#39;</span> <span class="o">%</span> <span class="n">direction</span>
    <span class="k">return</span>


<span class="n">left</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">burn</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;Left&#39;</span><span class="p">,</span> <span class="n">candle</span><span class="o">.</span><span class="n">popleft</span><span class="p">))</span>
<span class="n">right</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">burn</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;Right&#39;</span><span class="p">,</span> <span class="n">candle</span><span class="o">.</span><span class="n">pop</span><span class="p">))</span>

<span class="n">left</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="n">right</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>

<span class="n">left</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
<span class="n">right</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python collections_deque_both_ends.py
Left: 0
Right: 10
Left: 1
Right: 9
Left: 2
Right: 8
Left: 3
Right: 7
Left: 4
Right: 6
Left: 5
Right done
Left done</pre>
</div>
<p>另外一个双端队列有用的功能是在每一个方向上转动一些项, 以跳过某些项.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">collections</span>

<span class="n">d</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">(</span><span class="nb">xrange</span><span class="p">(</span><span class="mf">10</span><span class="p">))</span>
<span class="k">print</span> <span class="s">&#39;Normal :&#39;</span><span class="p">,</span> <span class="n">d</span>

<span class="n">d</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">(</span><span class="nb">xrange</span><span class="p">(</span><span class="mf">10</span><span class="p">))</span>
<span class="n">d</span><span class="o">.</span><span class="n">rotate</span><span class="p">(</span><span class="mf">2</span><span class="p">)</span> <span class="c">## 向右旋转两个元素</span>
<span class="k">print</span> <span class="s">&#39;Right rotation:&#39;</span><span class="p">,</span> <span class="n">d</span>

<span class="n">d</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">(</span><span class="nb">xrange</span><span class="p">(</span><span class="mf">10</span><span class="p">))</span>
<span class="n">d</span><span class="o">.</span><span class="n">rotate</span><span class="p">(</span><span class="o">-</span><span class="mf">2</span><span class="p">)</span> <span class="c">## 向左旋转两个元素</span>
<span class="k">print</span> <span class="s">&#39;Left rotation :&#39;</span><span class="p">,</span> <span class="n">d</span>
</pre></div>
</div>
<p>从右边旋转(使用一个正数)双端队列, 将项向右移动至右端末尾, 对于超过右边界的项又被移动到双端队列的左边. 从左边旋转(使用一个负数)双端队列, 将项向左边移至左端末尾, 对于超过左边界的项又被移动到双端队列的右边.</p>
<div class="highlight-python"><pre>$ python collections_deque_rotate.py
Normal : deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Right rotation: deque([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])
Left rotation : deque([2, 3, 4, 5, 6, 7, 8, 9, 0, 1])</pre>
</div>
</div>
<div class="section" id="defaultdict">
<h2>defaultdict<a class="headerlink" href="#defaultdict" title="Permalink to this headline">¶</a></h2>
<p>标准的字典包含了setdefault(), 用于设置一个默认值, 即当查找一个不存在的键值时用这个默认值来代替. 同样的, defaultdict能够让你在初始化时指定默认值.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">collections</span>

<span class="k">def</span> <span class="nf">default_factory</span><span class="p">():</span>
    <span class="k">return</span> <span class="s">&#39;default value&#39;</span>


<span class="n">d</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">defaultdict</span><span class="p">(</span><span class="n">default_factory</span><span class="p">,</span> <span class="n">foo</span><span class="o">=</span><span class="s">&#39;bar&#39;</span><span class="p">)</span>
<span class="k">print</span> <span class="n">d</span>
<span class="k">print</span> <span class="n">d</span><span class="p">[</span><span class="s">&#39;foo&#39;</span><span class="p">]</span>
<span class="k">print</span> <span class="n">d</span><span class="p">[</span><span class="s">&#39;bar&#39;</span><span class="p">]</span>
</pre></div>
</div>
<div class="highlight-python"><pre>$ python collections_defaultdict.py
defaultdict(&lt;function default_factory at 0x7ca70&gt;, {'foo': 'bar'})
bar
default value</pre>
</div>
<p>这个例子中, 所有键都使用相同的默认值.  当默认的是一个用于集成或累计值的类型, 如一个列表, 集合, 甚至是整型时会更有用处. 标准库文档包含了许多使用defaultdict的例子.</p>
<p>##更多的defaultdict例子</p>
<p>defaultdict的第一个参数default_factory, 提供了初始值, 默认为None, 余下的参数被看作是字典的键值对.</p>
<p>例子1: 字典值默认是一个空列表</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&#39;yellow&#39;</span><span class="p">,</span> <span class="mf">1</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="mf">2</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;yellow&#39;</span><span class="p">,</span> <span class="mf">3</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="mf">4</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="mf">1</span><span class="p">)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">s</span><span class="p">:</span>
<span class="go"> d[k].append(v)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="go">[(&#39;blue&#39;, [2, 4]), (&#39;red&#39;, [1]), (&#39;yellow&#39;, [1, 3])]</span>
</pre></div>
</div>
<p>例子2: 和上同样的效果, 只是使用了dict</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="p">{}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">s</span><span class="p">:</span>
<span class="go">    d.setdefault(k, []).append(v)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="go">[(&#39;blue&#39;, [2, 4]), (&#39;red&#39;, [1]), (&#39;yellow&#39;, [1, 3])]</span>
</pre></div>
</div>
<p>例子3: 值默认为整型, 其整型值默认为0</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="s">&#39;mississippi&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">s</span><span class="p">:</span>
<span class="go"> d[k] += 1</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="go">[(&#39;i&#39;, 4), (&#39;p&#39;, 2), (&#39;s&#39;, 4), (&#39;m&#39;, 1)]</span>
</pre></div>
</div>
<p>例子4:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">constant_factory</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
<span class="gp">... </span><span class="k">return</span> <span class="n">itertools</span><span class="o">.</span><span class="n">repeat</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">next</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="n">constant_factory</span><span class="p">(</span><span class="s">&#39;&lt;missing&gt;&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;John&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;ran&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;</span><span class="si">%(name)s</span><span class="s"> </span><span class="si">%(action)s</span><span class="s"> to </span><span class="si">%(object)s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">d</span> <span class="c">## 字符串格式化中, 还可以利用字典的key</span>
<span class="go">&#39;John ran to &lt;missing&gt;&#39;</span>
</pre></div>
</div>
<p>例子5:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="mf">1</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="mf">2</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="mf">3</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="mf">4</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="mf">1</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="mf">4</span><span class="p">)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="n">set</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">s</span><span class="p">:</span>
<span class="go"> d[k].add(v)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="go">[(&#39;blue&#39;, set([2, 4])), (&#39;red&#39;, set([1, 3]))]</span>
</pre></div>
</div>
</div>
<div class="section" id="id2">
<h2>参考<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><a class="reference external" href="http://en.wikipedia.org/wiki/Deque">Wikipedia: Deque</a></li>
<li><a class="reference external" href="http://docs.python.org/lib/deque-recipes.html">Deque Recipes</a></li>
<li><a class="reference external" href="http://docs.python.org/lib/defaultdict-examples.html">defaultdict examples</a></li>
<li><a class="reference external" href="http://jtauber.com/blog/2008/02/27/evolution_of_default_dictionaries_in_python/">James Tauber: Evolution of Default Dictionaries in Python</a></li>
<li><a class="reference external" href="http://www.doughellmann.com/projects/PyMOTW/">Python Module of the Week Home</a></li>
<li><a class="reference external" href="http://www.doughellmann.com/downloads/PyMOTW-1.46.tar.gz">Download Sample Code</a></li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="fileinput.html" title="PyMOTW: fileinput"
             >next</a> |</li>
        <li class="right" >
          <a href="atexit.html" title="PyMOTW: atexit"
             >previous</a> |</li>
        <li><a href="../index.html">PyMOTW Document v1.6 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 2009, vbarter &amp; liz.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
    </div>
  </body>
</html>